/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database.entitymanager;

import database.entity.ShopItem;
import database.entitymanager.exceptions.IllegalOrphanException;
import database.entitymanager.exceptions.NonexistentEntityException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import database.entity.ItemGroup;
import java.util.ArrayList;
import java.util.List;
import database.entity.Action;
import database.entity.CartItem;
import database.entity.ShopImage;

/**
 *
 * @author Stefan
 */
public class ShopItemManager {

  
    private static EntityManagerFactory emf = EntityFactoryHolder.getInstance();

    public static EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public static void create(ShopItem shopItem) {
        if (shopItem.getItemGroupList() == null) {
            shopItem.setItemGroupList(new ArrayList<ItemGroup>());
        }
        if (shopItem.getActionList() == null) {
            shopItem.setActionList(new ArrayList<Action>());
        }
        if (shopItem.getCartItemList() == null) {
            shopItem.setCartItemList(new ArrayList<CartItem>());
        }
        if (shopItem.getShopImageList() == null) {
            shopItem.setShopImageList(new ArrayList<ShopImage>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<ItemGroup> attachedItemGroupList = new ArrayList<ItemGroup>();
            for (ItemGroup itemGroupListItemGroupToAttach : shopItem.getItemGroupList()) {
                itemGroupListItemGroupToAttach = em.getReference(itemGroupListItemGroupToAttach.getClass(), itemGroupListItemGroupToAttach.getItemgroupid());
                attachedItemGroupList.add(itemGroupListItemGroupToAttach);
            }
            shopItem.setItemGroupList(attachedItemGroupList);
            List<Action> attachedActionList = new ArrayList<Action>();
            for (Action actionListActionToAttach : shopItem.getActionList()) {
                actionListActionToAttach = em.getReference(actionListActionToAttach.getClass(), actionListActionToAttach.getActionid());
                attachedActionList.add(actionListActionToAttach);
            }
            shopItem.setActionList(attachedActionList);
            List<CartItem> attachedCartItemList = new ArrayList<CartItem>();
            for (CartItem cartItemListCartItemToAttach : shopItem.getCartItemList()) {
                cartItemListCartItemToAttach = em.getReference(cartItemListCartItemToAttach.getClass(), cartItemListCartItemToAttach.getCartitemid());
                attachedCartItemList.add(cartItemListCartItemToAttach);
            }
            shopItem.setCartItemList(attachedCartItemList);
            List<ShopImage> attachedShopImageList = new ArrayList<ShopImage>();
            for (ShopImage shopImageListShopImageToAttach : shopItem.getShopImageList()) {
                shopImageListShopImageToAttach = em.getReference(shopImageListShopImageToAttach.getClass(), shopImageListShopImageToAttach.getShopimageid());
                attachedShopImageList.add(shopImageListShopImageToAttach);
            }
            shopItem.setShopImageList(attachedShopImageList);
            em.persist(shopItem);
            for (ItemGroup itemGroupListItemGroup : shopItem.getItemGroupList()) {
                itemGroupListItemGroup.getShopItemList().add(shopItem);
                itemGroupListItemGroup = em.merge(itemGroupListItemGroup);
            }
            for (Action actionListAction : shopItem.getActionList()) {
                actionListAction.getShopItemList().add(shopItem);
                actionListAction = em.merge(actionListAction);
            }
            for (CartItem cartItemListCartItem : shopItem.getCartItemList()) {
                ShopItem oldShopItemOfCartItemListCartItem = cartItemListCartItem.getShopItem();
                cartItemListCartItem.setShopItem(shopItem);
                cartItemListCartItem = em.merge(cartItemListCartItem);
                if (oldShopItemOfCartItemListCartItem != null) {
                    oldShopItemOfCartItemListCartItem.getCartItemList().remove(cartItemListCartItem);
                    oldShopItemOfCartItemListCartItem = em.merge(oldShopItemOfCartItemListCartItem);
                }
            }
            for (ShopImage shopImageListShopImage : shopItem.getShopImageList()) {
                ShopItem oldShopItemOfShopImageListShopImage = shopImageListShopImage.getShopItem();
                shopImageListShopImage.setShopItem(shopItem);
                shopImageListShopImage = em.merge(shopImageListShopImage);
                if (oldShopItemOfShopImageListShopImage != null) {
                    oldShopItemOfShopImageListShopImage.getShopImageList().remove(shopImageListShopImage);
                    oldShopItemOfShopImageListShopImage = em.merge(oldShopItemOfShopImageListShopImage);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public static void edit(ShopItem shopItem) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ShopItem persistentShopItem = em.find(ShopItem.class, shopItem.getItemid());
            List<ItemGroup> itemGroupListOld = persistentShopItem.getItemGroupList();
            List<ItemGroup> itemGroupListNew = shopItem.getItemGroupList();
            List<Action> actionListOld = persistentShopItem.getActionList();
            List<Action> actionListNew = shopItem.getActionList();
            List<CartItem> cartItemListOld = persistentShopItem.getCartItemList();
            List<CartItem> cartItemListNew = shopItem.getCartItemList();
            List<ShopImage> shopImageListOld = persistentShopItem.getShopImageList();
            List<ShopImage> shopImageListNew = shopItem.getShopImageList();
            List<String> illegalOrphanMessages = null;
            for (CartItem cartItemListOldCartItem : cartItemListOld) {
                if (!cartItemListNew.contains(cartItemListOldCartItem)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain CartItem " + cartItemListOldCartItem + " since its shopItem field is not nullable.");
                }
            }
            for (ShopImage shopImageListOldShopImage : shopImageListOld) {
                if (!shopImageListNew.contains(shopImageListOldShopImage)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain ShopImage " + shopImageListOldShopImage + " since its shopItem field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<ItemGroup> attachedItemGroupListNew = new ArrayList<ItemGroup>();
            for (ItemGroup itemGroupListNewItemGroupToAttach : itemGroupListNew) {
                itemGroupListNewItemGroupToAttach = em.getReference(itemGroupListNewItemGroupToAttach.getClass(), itemGroupListNewItemGroupToAttach.getItemgroupid());
                attachedItemGroupListNew.add(itemGroupListNewItemGroupToAttach);
            }
            itemGroupListNew = attachedItemGroupListNew;
            shopItem.setItemGroupList(itemGroupListNew);
            List<Action> attachedActionListNew = new ArrayList<Action>();
            for (Action actionListNewActionToAttach : actionListNew) {
                actionListNewActionToAttach = em.getReference(actionListNewActionToAttach.getClass(), actionListNewActionToAttach.getActionid());
                attachedActionListNew.add(actionListNewActionToAttach);
            }
            actionListNew = attachedActionListNew;
            shopItem.setActionList(actionListNew);
            List<CartItem> attachedCartItemListNew = new ArrayList<CartItem>();
            for (CartItem cartItemListNewCartItemToAttach : cartItemListNew) {
                cartItemListNewCartItemToAttach = em.getReference(cartItemListNewCartItemToAttach.getClass(), cartItemListNewCartItemToAttach.getCartitemid());
                attachedCartItemListNew.add(cartItemListNewCartItemToAttach);
            }
            cartItemListNew = attachedCartItemListNew;
            shopItem.setCartItemList(cartItemListNew);
            List<ShopImage> attachedShopImageListNew = new ArrayList<ShopImage>();
            for (ShopImage shopImageListNewShopImageToAttach : shopImageListNew) {
                shopImageListNewShopImageToAttach = em.getReference(shopImageListNewShopImageToAttach.getClass(), shopImageListNewShopImageToAttach.getShopimageid());
                attachedShopImageListNew.add(shopImageListNewShopImageToAttach);
            }
            shopImageListNew = attachedShopImageListNew;
            shopItem.setShopImageList(shopImageListNew);
            shopItem = em.merge(shopItem);
            for (ItemGroup itemGroupListOldItemGroup : itemGroupListOld) {
                if (!itemGroupListNew.contains(itemGroupListOldItemGroup)) {
                    itemGroupListOldItemGroup.getShopItemList().remove(shopItem);
                    itemGroupListOldItemGroup = em.merge(itemGroupListOldItemGroup);
                }
            }
            for (ItemGroup itemGroupListNewItemGroup : itemGroupListNew) {
                if (!itemGroupListOld.contains(itemGroupListNewItemGroup)) {
                    itemGroupListNewItemGroup.getShopItemList().add(shopItem);
                    itemGroupListNewItemGroup = em.merge(itemGroupListNewItemGroup);
                }
            }
            for (Action actionListOldAction : actionListOld) {
                if (!actionListNew.contains(actionListOldAction)) {
                    actionListOldAction.getShopItemList().remove(shopItem);
                    actionListOldAction = em.merge(actionListOldAction);
                }
            }
            for (Action actionListNewAction : actionListNew) {
                if (!actionListOld.contains(actionListNewAction)) {
                    actionListNewAction.getShopItemList().add(shopItem);
                    actionListNewAction = em.merge(actionListNewAction);
                }
            }
            for (CartItem cartItemListNewCartItem : cartItemListNew) {
                if (!cartItemListOld.contains(cartItemListNewCartItem)) {
                    ShopItem oldShopItemOfCartItemListNewCartItem = cartItemListNewCartItem.getShopItem();
                    cartItemListNewCartItem.setShopItem(shopItem);
                    cartItemListNewCartItem = em.merge(cartItemListNewCartItem);
                    if (oldShopItemOfCartItemListNewCartItem != null && !oldShopItemOfCartItemListNewCartItem.equals(shopItem)) {
                        oldShopItemOfCartItemListNewCartItem.getCartItemList().remove(cartItemListNewCartItem);
                        oldShopItemOfCartItemListNewCartItem = em.merge(oldShopItemOfCartItemListNewCartItem);
                    }
                }
            }
            for (ShopImage shopImageListNewShopImage : shopImageListNew) {
                if (!shopImageListOld.contains(shopImageListNewShopImage)) {
                    ShopItem oldShopItemOfShopImageListNewShopImage = shopImageListNewShopImage.getShopItem();
                    shopImageListNewShopImage.setShopItem(shopItem);
                    shopImageListNewShopImage = em.merge(shopImageListNewShopImage);
                    if (oldShopItemOfShopImageListNewShopImage != null && !oldShopItemOfShopImageListNewShopImage.equals(shopItem)) {
                        oldShopItemOfShopImageListNewShopImage.getShopImageList().remove(shopImageListNewShopImage);
                        oldShopItemOfShopImageListNewShopImage = em.merge(oldShopItemOfShopImageListNewShopImage);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = shopItem.getItemid();
                if (findShopItem(id) == null) {
                    throw new NonexistentEntityException("The shopItem with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public static void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ShopItem shopItem;
            try {
                shopItem = em.getReference(ShopItem.class, id);
                shopItem.getItemid();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The shopItem with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<CartItem> cartItemListOrphanCheck = shopItem.getCartItemList();
            for (CartItem cartItemListOrphanCheckCartItem : cartItemListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This ShopItem (" + shopItem + ") cannot be destroyed since the CartItem " + cartItemListOrphanCheckCartItem + " in its cartItemList field has a non-nullable shopItem field.");
            }
            List<ShopImage> shopImageListOrphanCheck = shopItem.getShopImageList();
            for (ShopImage shopImageListOrphanCheckShopImage : shopImageListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This ShopItem (" + shopItem + ") cannot be destroyed since the ShopImage " + shopImageListOrphanCheckShopImage + " in its shopImageList field has a non-nullable shopItem field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<ItemGroup> itemGroupList = shopItem.getItemGroupList();
            for (ItemGroup itemGroupListItemGroup : itemGroupList) {
                itemGroupListItemGroup.getShopItemList().remove(shopItem);
                itemGroupListItemGroup = em.merge(itemGroupListItemGroup);
            }
            List<Action> actionList = shopItem.getActionList();
            for (Action actionListAction : actionList) {
                actionListAction.getShopItemList().remove(shopItem);
                actionListAction = em.merge(actionListAction);
            }
            em.remove(shopItem);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public static List<ShopItem> findShopItemEntities() {
        return findShopItemEntities(true, -1, -1);
    }

    public static List<ShopItem> findShopItemEntities(int maxResults, int firstResult) {
        return findShopItemEntities(false, maxResults, firstResult);
    }

    private static List<ShopItem> findShopItemEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(ShopItem.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public static ShopItem findShopItem(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(ShopItem.class, id);
        } finally {
            em.close();
        }
    }

    public static int getShopItemCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<ShopItem> rt = cq.from(ShopItem.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
